package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}21:22
 */
public class interview61 {

    public static void main(String[] args) {
        new interview61().twoSum(2);
    }

    public double[] twoSum(int n) {
        //思路二：动态规划（人人为我）
        /*
            子问题：
            状态：二维表 dp[n][6*n]：第n个筛子的“当前和”的出现次数
            转移方程：dp[n][j]=dp[n][j-1]+dp[n][j-2]+...+dp[n][j-6]
        */
        int[][] dp = new int[n][6 * n];
        for (int j = 0; j < 6; ++j) {
            dp[0][j] = 1;
        } //初始状态
        for (int i = 1; i < n; ++i) { //处理行
            for (int j = i; j < (i + 1) * 6; ++j) {
                for (int dice = 1; dice < 7; ++dice) {
                    if (j - dice < 0) {
                        break;
                    }
                    dp[i][j] += dp[i - 1][j - dice];
                }
            }
        }
        double[] res = new double[5 * n + 1];
        int j = n - 1;
        for (int i = 0; i < res.length; ++i) {
            res[i] = dp[n - 1][j++] / Math.pow(6, n);
        }
        return res;
    }
}
